<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<%@ include file="/common.jsp" %>
<script type="text/javascript">
var treeGrid;
$(function() {
    treeGrid = $('#treeGrid').treegrid({
        url : '${pageContext.request.contextPath }/sys/organization/treeGrid',
        idField : 'id',
        treeField : 'name',
        parentField : 'pid',
        fit : true,
        fitColumns : false,
        border : false,
        frozenColumns : [[{
	            width : '150',
	            title : 'ID',
	            field : 'id'
	        }
        ]],
        columns : [ [ 
   		{
            width : '100',
            title : '组织名',
            field : 'name'
       },  
   		{
            width : '100',
            title : '地址',
            field : 'address'
       },  
   		{
            width : '100',
            title : '编号',
            field : 'code'
       },  
   		{
            width : '100',
            title : '图标',
            field : 'icon'
       },  
   		{
            width : '100',
            title : '父级主键',
            field : 'pid'
       },  
   		{
            width : '100',
            title : '排序',
            field : 'seq'
       },  
		{
            width : '130',
            title : '创建时间',
            field : 'createTime'
        },   
        {
            field : 'action',
            title : '操作',
            width : 250,
            formatter : function(value, row, index) {
                var str = '';
                    <shiro:hasPermission  name="sys/organization/edit">
                        str += $.formatString('<a href="javascript:void(0)" class="user-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-black\'" onclick="editFun(\'{0}\');" >编辑</a>', row.id);
                    </shiro:hasPermission>  
                    <shiro:hasPermission name="sys/organization/delete">
                        str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                        str += $.formatString('<a href="javascript:void(0)" class="user-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-black\'" onclick="deleteFun(\'{0}\');" >删除</a>', row.id);
                    </shiro:hasPermission> 
                if(str==''){
                	str = '您尚无[操作权限]';
                }    
                return str;
            }
        }] ],
        onLoadSuccess:function(data){
            $('.user-easyui-linkbutton-edit').linkbutton({text:'编辑',plain:true,iconCls:'fi-pencil icon-black'});
            $('.user-easyui-linkbutton-del').linkbutton({text:'删除',plain:true,iconCls:'fi-x icon-black'});
        },
        toolbar : '#toolbar'
    });
});

function addFun() {
    parent.$.modalDialog({
        title : '添加',
        width : 500,
        height : 300,
        href : '${pageContext.request.contextPath}/sys/organization/addPage',
        buttons : [ {
        		text : '取消添加',
            	iconCls:'fi-x icon-black',
            	handler:function(){
            		parent.$.modalDialog.handler.dialog('destroy');
            		parent.$.modalDialog.handler = undefined;
            	}
            },{
            text : '确定添加',
            iconCls:'fi-check icon-black',
            handler : function() {
                parent.$.modalDialog.openner_treeGrid = treeGrid;;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                var f = parent.$.modalDialog.handler.find('#addForm');
                f.submit();
            }
        } ]
    });
}

function deleteFun(id) {
    if (id != undefined) {
        treeGrid.treegrid('select', id);
    }
    var node = treeGrid.treegrid('getSelected');
    if (node) {
        parent.$.messager.confirm('询问', '删除当前部门会连同子部门一起删除!您是否要删除当前部门？', function(b) {
            if (b) {
                progressLoad();
                $.post('${pageContext.request.contextPath }/sys/organization/delete', {
                    id : node.id
                }, function(result) {
                    if (result.success) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        treeGrid.treegrid('reload');
                    }else{
                        parent.$.messager.alert('提示', result.msg, 'info');
                    }
                    progressClose();
                }, 'JSON');
            }
        });
    }
}

function editFun(id) {
    if (id != undefined) {
        treeGrid.treegrid('select', id);
    }
    var node = treeGrid.treegrid('getSelected');
    if (node) {
    	parent.$.modalDialog({
            title : '编辑',
            width : 500,
            height : 300,
            href : '${pageContext.request.contextPath }/sys/organization/editPage?id=' + node.id,
            buttons : [ {
            	text:'取消编辑',
            	iconCls:'fi-x icon-black',
            	handler:function(){
            		 parent.$.modalDialog.handler.dialog('close');
            	}
            },{
                text : '确认编辑',
                iconCls:'fi-check icon-black',
                handler : function() {
                    parent.$.modalDialog.openner_treeGrid = treeGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#editForm');
                    f.submit();
                }
            } ]
        });
    }
}
</script>
<body class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center',border:false" >
        <table id="treeGrid" ></table>
    </div>
    <div id="toolbar" style="display: none;">
        <shiro:hasPermission name="sys/organization/add">
            <a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" 
            	data-options="plain:true,iconCls:'fi-plus icon-black'">添加</a>
        </shiro:hasPermission>    
    </div>
</body>
</html>
